home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / fonttools / drawfont.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  210 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    drawfont - 
  19.  *        Draw an object font.
  20.  *
  21.  *                Paul Haeberli - 1990
  22.  *
  23.  */
  24. #include "gl.h"
  25. #include "device.h"
  26. #include "stdio.h"
  27. #include "objfnt.h"
  28. #include "path.h"
  29. #include "vect.h"
  30.  
  31. #define ZSCALE    (25.0)
  32.  
  33. float fgetmousex();
  34. float fgetmousey();
  35.  
  36. printdoc()
  37. {
  38.     fprintf(stderr,"\n");
  39.     fprintf(stderr,"drawfont instructions\n");
  40.     fprintf(stderr,"\n");
  41.     fprintf(stderr,"    1. Use the LEFTMOUSE and slide left to right to see a character big\n"); 
  42.     fprintf(stderr,"    2. Press the MIDDLEMOUSE and drag around to see a maginified verison of character\n");
  43.     fprintf(stderr,"\n");
  44. }
  45.  
  46. main(argc,argv)
  47. int argc;
  48. char **argv;
  49. {
  50.     objfnt *fnt;
  51.     short val;
  52.     float pos;
  53.     int i, opos, newpos;
  54.     int cmin, nchar;
  55.     char title[256];
  56.     float xpos, ypos;
  57.  
  58.     if( argc<2 ) {
  59.     fprintf(stderr,"usage: drawfont file.of\n");
  60.     exit(1);
  61.     } 
  62.     fontname(argv[1]);
  63.     fnt = readobjfnt(argv[1]);
  64.     keepaspect(5,4);
  65.     sprintf(title,"drawfont %s",argv[1]);
  66.     winopen(title);
  67.     subpixel(1);
  68.     RGBmode();
  69.     doublebuffer();
  70.     gconfig();
  71.     cpack(0x00ffffff);
  72.     clear();
  73.     swapbuffers();
  74.     makearray(fnt);
  75.     printdoc();
  76.     qdevice(ESCKEY);
  77.     qdevice(LEFTMOUSE);
  78.     qdevice(MIDDLEMOUSE);
  79.     qdevice(RIGHTMOUSE);
  80.     opos = -1;
  81.     cmin = fnt->charmin;
  82.     nchar = fnt->nchars;
  83.     while(1) {
  84.     switch(qread(&val)) {
  85.         case ESCKEY:
  86.         exit(0);
  87.         case REDRAW:
  88.         makearray(fnt);
  89.         break;
  90.         case LEFTMOUSE:
  91.         if(val) {
  92.             while(getbutton(LEFTMOUSE)) {
  93.             pos = fgetmousex();
  94.             newpos = cmin+(int)(nchar*pos);
  95.             if(newpos != opos) {
  96.                 showchar(fnt,newpos,0);
  97.                 opos = newpos;
  98.             }
  99.             }
  100.         }
  101.         break;
  102.         case MIDDLEMOUSE:
  103.         if(val) {
  104.             while(getbutton(MIDDLEMOUSE)) {
  105.             xpos = fgetmousex();
  106.             ypos = fgetmousey();
  107.             ortho2(-1.5/ZSCALE,13.5/ZSCALE,-2.0/ZSCALE,10.0/ZSCALE);
  108.             translate(-15.0*xpos,-12.0*ypos,0.0);
  109.             showchar(fnt,newpos,2);
  110.             }
  111.             ortho2(-1.5,13.5,-2.0,10.0);
  112.             showchar(fnt,newpos,0);
  113.         }
  114.         break;
  115.     }
  116.     }
  117. }
  118.  
  119. makearray(fnt)
  120. objfnt *fnt;
  121. {
  122.     reshapeviewport();
  123.     ortho2(-1.5,13.5,-2.0,10.0);
  124.     cpack(0x00ffffff);
  125.     clear();
  126.     drawobjfnt(fnt);
  127.     swapbuffers();
  128. }
  129.  
  130. showchar(fnt,c,psize)
  131. objfnt *fnt;
  132. int c, psize;
  133. {
  134.     int didit;
  135.     char str[2], doc[256];
  136.  
  137.     cpack(0x00ffffff);
  138.     clear();
  139.     cpack(0x000000ff);
  140.     rectf(2.0,2.0,2.05,2.05);
  141.     cpack(0x00000000);
  142.     fontmoveto(2.0,2.0);
  143.     fontsetsize(8.0);
  144.     str[0] = c;
  145.     str[1] = 0;
  146.     fontmoveto(2.0,2.0);
  147.     if(fontshow(str)) {
  148.     if(fnt->type == SP_TYPE || fnt->type == PO_TYPE) {
  149.         fontmoveto(2.0,2.0);
  150.         fntpointmode(psize);
  151.         fontshow(str);
  152.         fntpointmode(0);
  153.         cpack(0x00000000);
  154.     }
  155. #ifdef SHOWBBOX
  156.     fontmoveto(2.0,2.0);
  157.     bboxshow(str);
  158. #endif
  159.     }
  160.     cpack(0x00000000);
  161.     cmov2(0.0,0.0);
  162.     sprintf(doc,"Char code %d",c);
  163.     charstr(doc);
  164.     swapbuffers();
  165. }
  166.  
  167. drawobjfnt(of)
  168. objfnt *of;
  169. {
  170.     int i, c;
  171.     int x, y, nc, nx, ny, cmin;
  172.     char str[2];
  173.     float fscale;
  174.  
  175.     cmin = of->charmin;
  176.     nc = of->nchars;
  177.     if(nc<=96) {
  178.     nc = 96;
  179.     nx = 12;
  180.     } else {
  181.     nx = 12;
  182.     while((nx*((2*nx)/3))<nc)
  183.         nx+=3;
  184.     } 
  185.     ny = (2*nx)/3;
  186.     fscale = 12.0/nx;
  187.     fontsetsize(fscale);
  188.     cpack(0x00000000);
  189.     for(i=0; i<nc; i++) {
  190.     c = i+cmin;
  191.     x = i%nx;
  192.     y = ny-1-(i/nx);
  193.     fontmoveto(fscale*x,fscale*y);
  194.     str[0] = c;
  195.     str[1] = 0;
  196.         if(fontshow(str)) {
  197. #ifdef SHOWBBOX
  198.         cpack(0x000000ff);
  199.         fontmoveto((float)x,(float)y);
  200.         bboxshow(str);
  201.         cpack(0x00000000);
  202. #endif
  203.     } else {
  204.         cpack(0x000000ff);
  205.         rectf(x+0.40,y+0.40,x+0.60,y+0.60);
  206.         cpack(0x00000000);
  207.     }
  208.     }
  209. }
  210.